package com.dhcc.DRGsHosp.facade.impl;

import java.sql.Clob;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.QuotaApplication;
import com.dhcc.DRGsHosp.facade.QuotaFacade;
import com.dhcc.DRGsHosp.infra.util.MonitorResultUtil;

/**
 * @title		:QuotaFacadeImpl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2016年6月12日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl38")
public class QuotaFacadeImpl implements QuotaFacade {

	@Autowired
	private QuotaApplication quotaApplication;
	/**
	 * 
	 *  @Description：获取指标code相关信息
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#getQuotas(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public InvokeResult getQuotas(String roleCode,String userCode , String pageCode) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		//查询数据库，获取指标配置
		List<Object[]> list = quotaApplication.getQuotasUserByRole(roleCode,userCode,pageCode);
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("code", objs[0]);
			map.put("name", objs[4]);
			map.put("id", objs[2]);
			map.put("pid", objs[3]);
			if(objs[5]!=null){
				map.put("checked","1".equals(objs[5].toString())?true:false);
			}
			if(objs[6]!=null){
				map.put("open", "1".equals(objs[6].toString())?true:false);
			}
			if(objs[7]!=null){
				map.put("type", objs[7].toString());
			}
			if( objs.length >= 9 ) {
				map.put("islink", objs[8]);
			}
			if(objs[9]!=null){
				map.put("releate", "1".equals(objs[9].toString())?true:false);
			}
			
			resultList.add(map);
		}
		
		return InvokeResult.success(resultList);
	}
/**
 * 
 *  @Description：保存指标
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#saveQuotas(java.util.Map)
 */
	@Override
	public InvokeResult saveQuotas(Map<String, Object> json) {
		try{
			quotaApplication.saveQuotas(json);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
	/**
	 * 
	 *  @Description：添加新指标
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#saveQuota(java.util.Map)
	 */
	@Override
	public InvokeResult saveQuota(Map<String, Object> json) {
		try{
			return InvokeResult.success(quotaApplication.saveQuota(json));
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
	}
	
	
/**
 * 
 *  @Description：获取指标列表
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#getQuotaList(java.lang.String)
 */
	@Override
	public InvokeResult getQuotaList(String quotaType) {
		return InvokeResult.success(quotaApplication.getQuotaList(quotaType));
	}

	@Override
	public InvokeResult getQuotaByCode(String quotaCode) {
		List<Map<String, Object>> list = quotaApplication.getQuotaByCode(quotaCode);
//		for(Map<String, Object> objs: list){
//			Clob clob = (Clob) objs.get("SELECT_SQL");
//			try {
//				String sql = MonitorResultUtil.ClobToString(clob);
//				objs.put("SELECT_SQL", sql);
//			} catch (Exception e) {
//				e.printStackTrace();
//				objs.put("SELECT_SQL", "");
//			}
//		}
		return InvokeResult.success(list);
	}
	
	@Override
	public InvokeResult getQuotaByDesc(String quotaDesc) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Object[]> list = quotaApplication.getQuotaByDesc(quotaDesc);
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("quotaCode", objs[0]);
			map.put("quotaName", objs[1]!=null?objs[1]:"");
			map.put("quotaType", objs[2]!=null?objs[2]:"");
			map.put("unit", objs[3]!=null?objs[3].toString():"");
			Clob clob = (Clob) objs[4];
			try {
				String sql = MonitorResultUtil.ClobToString(clob);
				map.put("sql", sql);
			} catch (Exception e) {
				e.printStackTrace();
				map.put("sql", "");
			}
			map.put("qType", objs[5]!=null?objs[5]:"");
			resultList.add(map);
		}
		return InvokeResult.success(resultList);
	}

	@Override
	public InvokeResult delQuota(String quotaCode) {
		try{
			quotaApplication.delQuota(quotaCode);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
/**
 * 
 *  @Description：获取指标
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#getQuotaRelease(java.lang.String)
 */
	@Override
	public InvokeResult getQuotaRelease(String quotaCode) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Object[]> list = quotaApplication.getQuotaRelease(quotaCode);
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("quotaCode", objs[0]);
			map.put("quotaName", objs[1]!=null?objs[1]:"");
			map.put("quotaIndex", objs[2]!=null?objs[2]:"");
			resultList.add(map);
		}
		return InvokeResult.success(resultList);
	}

	@Override
	public InvokeResult saveQuotaRelease(Map<String, Object> json) {
		try{
			quotaApplication.saveQuotaRelease(json);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}

	@Override
	public InvokeResult delInvalidReleaseQuotas(String param, String quotaCode) {
		try{
			quotaApplication.delInvalidReleaseQuotas(param, quotaCode);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
/**
 * 
 *  @Description：清空角色下的指标配置，页面不清空
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#emptyQuotaSetting(java.lang.String, java.lang.String)
 */
	@Override
	public InvokeResult emptyQuotaSetting(String userCode, String roleCode) {
		try{
			quotaApplication.emptyQuotaSetting(userCode, roleCode);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}

	@Override
	public InvokeResult deleteQuotaSettingByCode(String quotaCode, String roleCode,String pid,String id) {
		try{
			quotaApplication.deleteQuotaSettingByCode(quotaCode, roleCode,pid,id);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}

	@Override
	public InvokeResult clearQuotaSettings(String userCode, String roleCode,
			String pageCode) {
		try{
			quotaApplication.clearQuotaSettings(userCode, roleCode, pageCode);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
	/**
	 * 
	 *  allUpdateQuotaSql.  
	 *  @Description：批量更新sql
	 *  @Author LiuHongjun
	 *  @return 
	 *  @Date:2017年7月20日下午2:09:09
	 */
	@Override
	public InvokeResult allUpdateQuotaSql() {
		try{
			quotaApplication.allUpdateQuotaSql();
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
/**
 * 
 *  @Description：根据角色获取配置的指标
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#getQuotasByRole(java.lang.String, java.lang.String)
 */
	@Override
	public InvokeResult getQuotasByRole(String roleCode, String userCode) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		//查询数据库，获取指标配置
		List<Object[]> list = quotaApplication.getQuotasByRole(roleCode, userCode);
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("code", objs[0]);
			map.put("name", objs[1]);
			map.put("id", objs[2]);
			map.put("pid", objs[3]);
			if(objs[4]!=null){
				map.put("checked","1".equals(objs[4].toString())?true:false);
			}
			if(objs[5]!=null){
				map.put("open", "1".equals(objs[5].toString())?true:false);
			}
			if(objs[6]!=null){
				map.put("type", objs[6].toString());
			}
			resultList.add(map);
		}		
		return InvokeResult.success(resultList);
	}
/**
 * 
 *  @Description：：获取用户角色授权的添加列表
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#getQuotaRoleList(java.lang.String)
 */
@Override
public InvokeResult getQuotaRoleList(String quotaType) {
	return InvokeResult.success(quotaApplication.getQuotaRoleList(quotaType));
}
/**
 * 
 *  @Description：存储管理员下配置指标
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#adminSaveQuotas(java.util.Map)
 */
@Override
public InvokeResult adminSaveQuotas(Map<String, Object> json) {
	try{
		quotaApplication.adminSaveQuotas(json);
	}catch(Exception e){
		return InvokeResult.failure(e.getMessage());
	}
	return InvokeResult.success();
}
/**
 * 
 *  @Description：页面指标配置更新
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.QuotaFacade#updatePagesQuotas(java.util.Map)
 */
@Override
public InvokeResult updatePagesQuotas(Map<String, Object> json) {
	try{
		quotaApplication.updatePagesQuotas(json);
	}catch(Exception e){
		return InvokeResult.failure(e.getMessage());
	}
	return InvokeResult.success();
}
}
